package com.yun.baymax.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;

import com.yun.baymax.R;
import com.yun.baymax.adapter.NoteAdapter;
import com.yun.baymax.bean.adapter.NoteInfo;
import com.yun.lib.utils.L;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

public class NoteActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {
    private RecyclerView mRecyclerView;
    private NoteAdapter adapter;
    private LayoutInflater inflater;
    private SwipeRefreshLayout refreshLayout;

    @Override
    protected int setLayoutResourceId() {
        return R.layout.activity_note;
    }

    @Override
    protected void initVariables() {
        inflater = getLayoutInflater();
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        toolbar.setTitle("记事本");
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivityForResult(new Intent(mContext, NoteEditActivity.class), 1);
            }
        });
        mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        refreshLayout = (SwipeRefreshLayout) findViewById(R.id.refresh);
        refreshLayout.setColorSchemeResources(R.color.colorPrimary, R.color.text_color_blue, R.color.voucher_color_red);
        refreshLayout.setOnRefreshListener(this);

        StaggeredGridLayoutManager staggeredLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(staggeredLayoutManager);

        adapter = new NoteAdapter(mContext, null);
        mRecyclerView.setAdapter(adapter);
    }

    @Override
    protected void loadData() {
        refreshLayout.setRefreshing(true);
        BmobQuery<NoteInfo> billQuery = new BmobQuery<>();
        billQuery.addWhereEqualTo("author", BmobUser.getCurrentUser().getUsername());
        billQuery.findObjects(new FindListener<NoteInfo>() {
            @Override
            public void done(List<NoteInfo> list, BmobException e) {
                refreshLayout.setRefreshing(false);
                if (e == null) {
                    adapter.setDatas(list);
                    L.i("note_get");
                } else {
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        loadData();
    }

    @Override
    public void onRefresh() {
        loadData();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        loadData();
    }
}
